﻿using GKQC.Common;
using GKQC.DbHelper.AppViews;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace GKQC.DbHelper.Services
{
    /// <summary>
    /// application/json
    /// </summary>
    [Serializable]
    public class ApiHelper
    {
        public static string POST { get; private set; } = "POST";
        public static string GET { get; private set; } = "GET";
        static string accept = "text/html, application/xhtml+xml, */*";
        static string contentType = "application/json";

        public static string LoginAddress { get; private set; } = "/api/user/login";
        public static string SplitAddress { get; private set; } = "/api/label/split";
        public static string SaveAddress { get; private set; } = "/api/label/savescan";
        public static string StockbillAddress { get; private set; } = "/api/stockbill/billno";


        /// <summary>
        /// 调用WEBAPI方法
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="body">参数</param>
        /// <param name="method">POST or GET</param>
        /// <returns></returns>
        public static string HttpPost(string url, string body, string method)
        {
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = method;
            request.Accept = accept;
            request.ContentType = contentType;

            byte[] buffer = encoding.GetBytes(body);
            request.ContentLength = buffer.Length;
            if (method.Equals(POST))
            {
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }

        /// <summary>
        /// 调用WEBAPI方法
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="body">参数</param>
        /// <param name="method">POST or GET</param>
        /// <returns></returns>
        public static string HttpPost2(string url, string body, string method)
        {
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = method;
            request.Accept = accept;
            //request.ContentType = contentType;

            byte[] buffer = encoding.GetBytes(body);
            request.ContentLength = buffer.Length;
            if (method.Equals(POST))
            {
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }

        /// <summary>
        /// 出入库单查询参数转换
        /// </summary>
        /// <param name="formName"></param>
        /// <param name="userCode"></param>
        /// <param name="password"></param>
        /// <param name="billNo"></param>
        /// <param name="customerCode"></param>
        /// <param name="isInnerLabel"></param>
        /// <param name="scanData"></param>
        /// <returns></returns>
        public static string GetLoginParam(string formName, string userCode, string password)
        {
            QueryParam p = new QueryParam();
            p.FormName = formName;
            p.UserCode = userCode;
            p.Password = password;
            p.OnlineId = string.Empty;
            p.Data = string.Empty;

            return p.ToJson();
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url"></param>
        /// <param name="fileName">要保存的目标文件名</param>
        /// <returns></returns>
        public static bool DownloadFile(string url, string fileName)
        {
            System.Net.WebClient client = new System.Net.WebClient();
            byte[] data = client.DownloadData(url);

            FileStream fs = new FileStream(fileName, FileMode.Create);
            fs.Write(data, 0, data.Length);
            fs.Close();

            return true;
        }
    }
}
